<?php
session_start();

// 检查是否已安装
if (!file_exists(__DIR__ . '/../config/config.php')) {
    header('Location: ../install.php');
    exit;
}

require_once __DIR__ . '/../config/config.php';
require_once __DIR__ . '/../config/admin.php';

// 检查管理员是否已登录
if (!isset($_SESSION['admin_logged_in']) || !$_SESSION['admin_logged_in']) {
    header('Location: login.php');
    exit;
}

// 处理更新配置的请求
if ($_POST && isset($_POST['update_config'])) {
    $api_key = $_POST['api_key'] ?? $config['api_key'];
    $base_url = $_POST['base_url'] ?? $config['base_url'];
    $default_model = $_POST['default_model'] ?? $config['default_model'];
    
    // 更新配置文件
    $config_content = "<?php\n";
    $config_content .= "\$config = [\n";
    $config_content .= "    'api_key' => '" . addslashes($api_key) . "',\n";
    $config_content .= "    'base_url' => '" . addslashes($base_url) . "',\n";
    $config_content .= "    'default_model' => '" . addslashes($default_model) . "',\n";
    $config_content .= "];\n";
    
    file_put_contents(__DIR__ . '/../config/config.php', $config_content);
    
    // 重新加载配置
    $config = [
        'api_key' => $api_key,
        'base_url' => $base_url,
        'default_model' => $default_model
    ];
    
    $message = '配置已更新成功！';
}

// 获取所有用户
$users_dir = __DIR__ . '/../users/';
$users = [];
if (is_dir($users_dir)) {
    foreach (scandir($users_dir) as $user_dir) {
        if ($user_dir !== '.' && $user_dir !== '..' && is_dir($users_dir . $user_dir)) {
            $profile_file = $users_dir . $user_dir . '/profile.json';
            if (file_exists($profile_file)) {
                $profile = json_decode(file_get_contents($profile_file), true);
                $users[] = [
                    'ip' => $user_dir,
                    'nickname' => $profile['nickname'],
                    'created_at' => $profile['created_at']
                ];
            }
        }
    }
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>lanxAI - 管理员面板</title>
    <link rel="stylesheet" href="../assets/style.css">
</head>
<body>
    <div class="admin-container">
        <div class="header">
            <h1>lanxAI - 管理员面板</h1>
        </div>
        
        <div class="nav">
            <a href="#" class="active">系统配置</a>
            <a href="#users">用户管理</a>
            <a href="#logs">日志查看</a>
            <a href="../index.php">返回前台</a>
            <a href="logout.php" class="logout">退出登录</a>
        </div>
        
        <div class="content">
            <?php if (isset($message)): ?>
                <div class="message-success"><?php echo $message; ?></div>
            <?php endif; ?>
            
            <h2>系统配置</h2>
            <form method="post">
                <input type="hidden" name="update_config" value="1">
                
                <div class="form-group">
                    <label for="api_key">API Key</label>
                    <input type="password" id="api_key" name="api_key" value="<?php echo htmlspecialchars($config['api_key']); ?>" required>
                </div>
                
                <div class="form-group">
                    <label for="base_url">Base URL</label>
                    <input type="text" id="base_url" name="base_url" value="<?php echo htmlspecialchars($config['base_url']); ?>" required>
                </div>
                
                <div class="form-group">
                    <label for="default_model">默认AI模型</label>
                    <input type="text" id="default_model" name="default_model" value="<?php echo htmlspecialchars($config['default_model']); ?>" required>
                    <small>例如: gpt-3.5-turbo, gpt-4, claude-3-sonnet 等</small>
                </div>
                
                <button type="submit" class="btn">更新配置</button>
            </form>
            
            <h2 style="margin-top: 30px;">用户管理</h2>
            <table>
                <thead>
                    <tr>
                        <th>昵称</th>
                        <th>IP地址</th>
                        <th>注册时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($users as $user): ?>
                    <tr>
                        <td><?php echo htmlspecialchars($user['nickname']); ?></td>
                        <td><?php echo $user['ip']; ?></td>
                        <td><?php echo $user['created_at']; ?></td>
                        <td>
                            <button class="btn" onclick="viewUser('<?php echo $user['ip']; ?>')">查看聊天记录</button>
                            <button class="btn" onclick="viewUserb('<?php echo $user['ip']; ?>')">查看角色设定</button>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        
        <div class="footer">
            LANX开源AI
        </div>
    </div>
    
    <script>
        function viewUser(ip) {
            // 在新窗口中打开用户聊天记录
            window.open('../users/' + ip + '/history.json', '_blank');
        }
        function viewUserb(ip) {
            // 在新窗口中打开用户聊天记录
            window.open('../users/' + ip + '/prompt.txt', '_blank');
        }
    </script>
</body>
</html>
